package com.jftone.bh.app.util;

import java.util.List;

import org.jftone.util.IData;
import org.jftone.util.StringUtil;

public class TempletUtil {
	private static final String TEMPLET_VARIABLE="#(\\w+)#";
	private static final String FIX_STR="#";
	
	/**
	 *  解析短信模板
	 * @param templetContent
	 * @return
	 */
	public static String[] parseTemplet(String templetContent){
		List<String> params = StringUtil.getMathchList(templetContent, TEMPLET_VARIABLE);
		if(null == params || params.isEmpty()){
			return null;
		}
		String[] rets = new String[params.size()];
		int i=0;
		for(String param : params){
			String[] keys = StringUtil.getMathchGroup(param, TEMPLET_VARIABLE);
			if(keys !=null && keys.length>0){
				rets[i] = keys[0];
				i++;
			}
		}
		return rets;
	}
	
	public static String getParseContent(String templetContent, IData<String, Object> data){
		String[] params = TempletUtil.parseTemplet(templetContent);
		if(null == params){
			return "";
		}
		String retContent = templetContent;
		for(String key : params){
			retContent = retContent.replace(FIX_STR+key+FIX_STR, data.getString(key));
		}
		return retContent;
	}
}
